import { getDetailInfo, addToCartOfUpdate } from '@/api/index.js'
import { getUUId } from '@/utils/uuid_token'
const state = {
  detailInfo: {},
  uuid:getUUId()
}
const mutations = {
  GETDETAILINFOFN(state, detailInfo) {
    state.detailInfo = detailInfo
  },
}
const actions = {
  async getDetailInfoFn({ commit }, payload) {
    let result = await getDetailInfo(payload)
    if (result.code == 200) {
      commit('GETDETAILINFOFN', result.data)
    }
  },
  async addToCartOfUpdate({ commit }, { skuId, skuNum }) {
    let result = await addToCartOfUpdate(skuId,skuNum)
    if (result.code == 200) {
      return 'ok'
    } else {
      return Promise.reject(new Error('fail'))
    }
  }
}
const getters = {
  categoryView(state) {
    //第一次加载时，detailInfo可能为空就会访问不到 categoryView，所以返回默认空对象
    return state.detailInfo.categoryView||{}
  },
  skuInfo(state) {
    return state.detailInfo.skuInfo||{}
  }
}
export default {
  state,
  mutations,
  actions,
  getters,
}
